/**
 * Created by PhpStorm.
 * User:  iyahe@qq.com (天明)
 * Date: 2019/8/1 0001
 * Time: 下午 18:10
 * Description:
 */
var request = require('superagent');
var agents = require('./config/ua')
require('superagent-proxy')(request);
let proxy = {
	init (web, app) {
		// TODO 挂载方法到 app 模块上，供 remote 模块使用
		app.proxyTest = function (
			$url, $proxy = '', $option = {
				timeout: {
					response: 3000,
					deadline: 6000
				}
			}) {
			// TODO 随机获取一个ua
			let userAgent = agents[parseInt(Math.random() * agents.length)];
			return new Promise((resolve, reject) => {
				request
				.get($url)
				.timeout($option.timeout)
				.set({ 'User-Agent': userAgent })
				.proxy($proxy)
				.end(function (err, res) {
					if (err) {
						reject(err)
					} else {
						resolve(res.status, res.body, res.headers)
					}
				});
			})
		}
	}
}


exports.default = proxy
